package com.example.ten;


import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.widget.RadioGroup;
import com.example.ten.article.ArticleFragment;
import com.example.ten.image.ImageFragment;
import com.example.ten.me.MeFragment;
import com.example.ten.moive.MoiveFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
   RadioGroup mainRg;
   Fragment movieFrag,articleFrag,imageFrag,meFrag;
   List<Fragment>fragments;
   FragmentManager fm;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mainRg=findViewById(R.id.main_rg);
        mainRg.setOnCheckedChangeListener(this);
        fragments = new ArrayList<>();
        movieFrag = new MoiveFragment();
        articleFrag = new ArticleFragment();
        imageFrag = new ImageFragment();
        meFrag = new MeFragment();
        addFragmentPage();
    }

    private void addFragmentPage() {
        fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.add(R.id.main_layout,movieFrag);
        transaction.add(R.id.main_layout,articleFrag);
        transaction.add(R.id.main_layout,imageFrag);
        transaction.add(R.id.main_layout,meFrag);
        transaction.hide(articleFrag);
        transaction.hide(imageFrag);
        transaction.hide(meFrag);
        transaction.commit();
        fragments.add(movieFrag);
        fragments.add(articleFrag);
        fragments.add(imageFrag);
        fragments.add(meFrag);

    }



    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.main_rb_moive:
                mangerFragment(movieFrag);
                break;
            case R.id.main_rb_article:
                mangerFragment(articleFrag);
                break;
            case R.id.main_rb_image:
                mangerFragment(imageFrag);
                break;
            case R.id.main_rb_me:
                mangerFragment(meFrag);
                break;
        }
    }

    private void mangerFragment(Fragment showFragment){
        FragmentTransaction transaction = fm.beginTransaction();
        for (int i = 0; i < fragments.size(); i++) {
            transaction.hide(fragments.get(i));
        }
        transaction.show(showFragment);
        transaction.commit();
    }
}
